/**
 * 
 */
package com.lanswon.qzsmk.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.junit.Test;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

import com.lanswon.qzsmk.exception.SystemException;
import com.lanswon.qzsmk.model.Personal;
import com.lanswon.qzsmk.model.PersonalTmp;

/**
 * @author sun
 *
 */
public class CopyBean {
    private final static Logger logger = (Logger) LoggerFactory.getLogger(CopyBean.class); 

    
    @Test
    public void testCopy(){
    	PersonalTmp pt = new PersonalTmp();
    	pt.setCustomerNo("1111111111");
    	pt.setCustomerName("张三");
    	
    	Personal p = new Personal();
    	
    	p = (Personal) copyA2b(pt, p);
    	System.out.println(p.getCustomerNo()+":"+p.getCustomerName());
    }
	
	/**
	 * 两个javaBean 之间赋值 （objA、objB javaBean相同或类似） 
	 * 将objA对象赋值给objB。 这里只将自身的属性进行赋值
	 * @param objA
	 * @param objB
	 * @return
	 */
	public static Object copyA2b(Object objA, Object objB)  {
		Class<?> clazz = objA.getClass() ;  
	    Field[] field1 = clazz.getDeclaredFields() ; 
        for(int i=0 ; i<field1.length ; i++){
        	String name = field1[i].getName();
        		 try {
        				Class<?> classB = objB.getClass();
        				String mb = getGetterName(name);
        				Method m2 = classB.getMethod(mb);
        			
        			 
 					Method m1 = objA.getClass().getMethod(getGetterName(name));
 					Object value = (Object) m1.invoke(objA);
 					Object value1 = (Object) m2.invoke(objB);
 					m2 = objB.getClass().getMethod(getSetterName(name), new Class[] { field1[i].getType() });				
 					m2.invoke(objB, value);
     				if(value1 == null){//如果B里的属性不为空  ，属性就不从A复制到B了					
     					m2.invoke(objB, value);
     				}
     				
         	 	} catch (NoSuchMethodException e) {
         	 		logger.debug("过滤目标对象没有的属性");
         	 		e.printStackTrace();
         	 		continue;
	
				}catch (Exception e) {
         	 		e.printStackTrace();	
         	 		String msg="复制对象异常";
         			logger.error(msg);
         			throw new SystemException(SystemException.CopyException, msg);
         		}			
        }
        return objB;
	}
	
	public static Object copyA2b_(Object objA, Object objB)  {
		Class<?> clazz = objA.getClass() ;  
	    Field[] field1 = clazz.getDeclaredFields() ; 
        for(int i=0 ; i<field1.length ; i++){       	
        	String name = field1[i].getName();
	        	 try {
					Method m2 = objB.getClass().getMethod(getGetterName(name));
					Method m1 = objA.getClass().getMethod(getGetterName(name));
					Object value = (Object) m1.invoke(objA);
					Object value1 = (Object) m2.invoke(objB);
					m2 = objB.getClass().getMethod(getSetterName(name), new Class[] { field1[i].getType() });				
					m2.invoke(objB, value);
	        	 	} catch (NoSuchMethodException e) {
	        	 		e.printStackTrace();
	        	 		
	
				}catch (Exception e) {
					e.printStackTrace();
        	 		String msg="复制对象异常";
        			logger.error(msg);
        			throw new SystemException(SystemException.CopyException, msg);

			}
        }
        return objB;
	}
	
	/**
	 * 根据变量名得到get方法
	 * 
	 * @param propertyName
	 * @return
	 */
	private static String getGetterName(String propertyName) {
		String method = "get" + propertyName.substring(0, 1).toUpperCase()
				+ propertyName.substring(1);
		return method;
	}
	
	/**
	 * 得到setter方法
	 * 
	 * @param propertyName
	 *            变量名
	 * @return
	 */
	private static String getSetterName(String propertyName) {
		String method = "set" + propertyName.substring(0, 1).toUpperCase()
				+ propertyName.substring(1);
		return method;
	}
}
